/*
 * BorderSplitPane.java
 */

package xq.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

/**
 *
 * @author ThorntonRP
 */
public class BorderSplitPane extends JPanel {

	private static final long serialVersionUID = -5393597275030799120L;

	/**
	 * Used to add a <code>Component</code> at the center of the
	 * <code>BorderSplitPane</code>.
	 */
	public final static String CENTER = "center";

	/**
	 * Used to add a <code>Component</code> on the north (top) side of the
	 * <code>BorderSplitPane</code>.
	 */
	public final static String NORTH = "north";

	/**
	 * Used to add a <code>Component</code> on the south (bottom) side of the
	 * <code>BorderSplitPane</code>.
	 */
	public final static String SOUTH = "south";

	/**
	 * Used to add a <code>Component</code> on the east (right) side of the
	 * <code>BorderSplitPane</code>.
	 */
	public final static String EAST = "east";

	/**
	 * Used to add a <code>Component</code> on the west (left) side of the
	 * <code>BorderSplitPane</code>.
	 */
	public final static String WEST = "west";

	private final static int CENTER_INDEX = 0;
	private final static int NORTH_INDEX  = 1;
	private final static int SOUTH_INDEX  = 2;
	private final static int EAST_INDEX   = 3;
	private final static int WEST_INDEX   = 4;

	private Component centerComponent;
	private Component northComponent;
	private Component southComponent;
	private Component eastComponent;
	private Component westComponent;

	private JSplitPane northSplitPane;
	private JSplitPane southSplitPane;
	private JSplitPane eastSplitPane;
	private JSplitPane westSplitPane;

	private int dividerSize;


	public BorderSplitPane() {
		init();
	}

	public BorderSplitPane(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
		init();
	}


	public int getDividerSize() {
		return dividerSize;
	}

	public void setDividerSize(int dividerSize) {
		this.dividerSize = dividerSize;
		northSplitPane.setDividerSize(dividerSize);
		southSplitPane.setDividerSize(dividerSize);
		eastSplitPane.setDividerSize(dividerSize);
		westSplitPane.setDividerSize(dividerSize);
	}


	public int getNorthDividerLocation() {
		return northSplitPane.getDividerLocation();
	}

	public int getSouthDividerLocation() {
		return southSplitPane.getDividerLocation();
	}

	public int getEastDividerLocation() {
		return eastSplitPane.getDividerLocation();
	}

	public int getWestDividerLocation() {
		return westSplitPane.getDividerLocation();
	}


	public void setNorthDividerLocation(int location) {
		northSplitPane.setDividerLocation(location);
	}

	public void setSouthDividerLocation(int location) {
		southSplitPane.setDividerLocation(location);
	}

	public void setEastDividerLocation(int location) {
		eastSplitPane.setDividerLocation(location);
	}

	public void setWestDividerLocation(int location) {
		westSplitPane.setDividerLocation(location);
	}


	public Component getCenterComponent() {
		return centerComponent;
	}

	public Component getNorthComponent() {
		return northComponent;
	}

	public Component getSouthComponent() {
		return southComponent;
	}

	public Component getEastComponent() {
		return eastComponent;
	}

	public Component getWestComponent() {
		return westComponent;
	}


	public void setCenterComponent(Component comp) {
		if (comp == null) {
			if (centerComponent != null) {
				remove(centerComponent);
				centerComponent = null;
			}
		} else {
			addImpl(comp, BorderSplitPane.CENTER, -1);
		}
	}

	public void setNorthComponent(Component comp) {
		if (comp == null) {
			if (northComponent != null) {
				remove(northComponent);
				northComponent = null;
			}
		} else {
			addImpl(comp, BorderSplitPane.NORTH, -1);
		}
	}

	public void setSouthComponent(Component comp) {
		if (comp == null) {
			if (southComponent != null) {
				remove(southComponent);
				southComponent = null;
			}
		} else {
			addImpl(comp, BorderSplitPane.SOUTH, -1);
		}
	}

	public void setEastComponent(Component comp) {
		if (comp == null) {
			if (eastComponent != null) {
				remove(eastComponent);
				eastComponent = null;
			}
		} else {
			addImpl(comp, BorderSplitPane.EAST, -1);
		}
	}

	public void setWestComponent(Component comp) {
		if (comp == null) {
			if (westComponent != null) {
				remove(westComponent);
				westComponent = null;
			}
		} else {
			addImpl(comp, BorderSplitPane.WEST, -1);
		}
	}


	@Override
	protected void addImpl(Component comp, Object constraints, int index) {
		if (constraints != null && !(constraints instanceof String)) {
			throw new IllegalArgumentException("Cannot add to layout: constraint must be a string (or null)");
		}

		// If constraints are null, put the component at the center.
		if (constraints == null) {
			switch (index) {
				case NORTH_INDEX:
					constraints = BorderSplitPane.NORTH;
					break;
				case SOUTH_INDEX:
					constraints = BorderSplitPane.SOUTH;
					break;
				case EAST_INDEX:
					constraints = BorderSplitPane.EAST;
					break;
				case WEST_INDEX:
					constraints = BorderSplitPane.WEST;
					break;
				case CENTER_INDEX:
				default:
					constraints = BorderSplitPane.CENTER;
					break;
			}
		}

		if (constraints.equals(BorderSplitPane.NORTH)) {
			northComponent = comp;
			northSplitPane.setDividerSize(dividerSize);
			northSplitPane.setTopComponent(comp);
		} else if (constraints.equals(BorderSplitPane.SOUTH)) {
			southComponent = comp;
			southSplitPane.setDividerSize(dividerSize);
			southSplitPane.setBottomComponent(comp);
		} else if (constraints.equals(BorderSplitPane.EAST)) {
			eastComponent = comp;
			eastSplitPane.setDividerSize(dividerSize);
			eastSplitPane.setRightComponent(comp);
		} else if (constraints.equals(BorderSplitPane.WEST)) {
			westComponent = comp;
			westSplitPane.setDividerSize(dividerSize);
			westSplitPane.setLeftComponent(comp);
		} else if (constraints.equals(BorderSplitPane.CENTER)) {
			centerComponent = comp;
			westSplitPane.setDividerSize(dividerSize);
			westSplitPane.setRightComponent(comp);
		} else {
			throw new IllegalArgumentException("Cannot add to layout: " +
					"constraint must be one of north, south, east, west, center.");
		}
	}


	@Override
	public void remove(Component comp) {
		if (comp == northComponent) {
			northComponent = null;
			northSplitPane.remove(comp);
			northSplitPane.setDividerSize(0);
		} else if (comp == southComponent) {
			southComponent = null;
			southSplitPane.remove(comp);
			southSplitPane.setDividerSize(0);
		} else if (comp == eastComponent) {
			eastComponent = null;
			eastSplitPane.remove(comp);
			eastSplitPane.setDividerSize(0);
		} else if (comp == westComponent) {
			westComponent = null;
			westSplitPane.remove(comp);
			westSplitPane.setDividerSize(0);
		} else if (comp == centerComponent) {
			centerComponent = null;
			westSplitPane.remove(comp);
			westSplitPane.setDividerSize(0);
		}
	}

	@Override
	public void remove(int index) {
		switch (index) {
			case NORTH_INDEX:
				remove(northComponent);
				break;
			case SOUTH_INDEX:
				remove(southComponent);
				break;
			case EAST_INDEX:
				remove(eastComponent);
				break;
			case WEST_INDEX:
				remove(westComponent);
				break;
			case CENTER_INDEX:
			default:
				remove(centerComponent);
				break;
		}
	}

	@Override
	public void removeAll() {
		remove(centerComponent);
		remove(northComponent);
		remove(southComponent);
		remove(eastComponent);
		remove(westComponent);
	}


	@Override
	public boolean isValidateRoot() {
		return true;
	}


	private void init() {

		northSplitPane = new SplitPane();
		southSplitPane = new SplitPane();
		eastSplitPane = new SplitPane();
		westSplitPane = new SplitPane();

		dividerSize = northSplitPane.getDividerSize();

		northComponent = northSplitPane.getTopComponent();
		southComponent = southSplitPane.getBottomComponent();
		eastComponent = eastSplitPane.getRightComponent();
		westComponent = westSplitPane.getLeftComponent();
		centerComponent = westSplitPane.getRightComponent();

		((JButton) northComponent).setText("north button");
		((JButton) southComponent).setText("south button");
		((JButton) eastComponent).setText("east button");
		((JButton) westComponent).setText("west button");
		((JButton) centerComponent).setText("center button");

		northSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		northSplitPane.setBottomComponent(eastSplitPane);

		southSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		southSplitPane.setResizeWeight(1.0);
		southSplitPane.setTopComponent(northSplitPane);

		eastSplitPane.setResizeWeight(1.0);
		eastSplitPane.setLeftComponent(westSplitPane);

		setLayout(new BorderLayout());
		super.addImpl(southSplitPane, BorderLayout.CENTER, -1);
	}
}
